<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>客户管理</el-breadcrumb-item>
      <el-breadcrumb-item>客户活动</el-breadcrumb-item>
      <el-breadcrumb-item>慰问信</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-input
        class="inputInfo"
        type="textarea"
        :rows="2"
        placeholder="请输入内容"
        v-model="sendEmailForm.info"
        :autosize="{ minRows: 17, maxRows: 17 }"
      >
      </el-input>
      <el-form :inline="true" :model="sendEmailForm">
        <el-form-item label="客户名称">
          <el-select
            v-model="sendEmailForm.clientName"
            placeholder="客户名称"
            @change="getEmail"
          >
            <el-option
              v-for="client in clientList"
              :key="client.clientId"
              :label="client.clientName"
              :value="client.clientName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户邮箱">
          <el-input
            v-model="sendEmailForm.clientEmail"
            placeholder="客户邮箱"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="sendBtn" type="primary" @click="sendEmail"
            >发送</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      clientList: [],
      sendEmailForm: {
        clientName: "",
        clientEmail: "",
        info: "",
      },
    };
  },
  methods: {
    //获取客户信息
    async getClientList() {
      this.loading = true;
      const { data: res } = await this.$http.get("clientUser");
      if (res.status !== 200) return this.$message.error(res.msg);
      this.clientList = res.clientList;
      this.loading = false;
    },
    getEmail(clientName) {
      const res = this.clientList.filter((item) => {
        if (item.clientName === clientName) return item;
      });
      this.sendEmailForm.clientEmail = res[0].clientEmail;
    },

    async sendEmail() {
      if (this.sendEmailForm.clientName === "")
        return this.$message.error("请选择发送对象");
      if (this.sendEmailForm.info === "")
        return this.$message.error("请输入内容");
      const confirmResult = await this.$confirm("是否确认发送？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (confirmResult != "confirm") return this.$message.info("取消发送");
      this.sendEmailForm.clientName = "";
      this.sendEmailForm.clientEmail = "";
      this.sendEmailForm.info = "";
      this.$message.success("已发送");
    },
  },
  created() {
    this.getClientList();
  },
};
</script>

<style scoped>
.el-card {
  position: relative;
  height: 600px;
}
.el-form {
  position: absolute;
  bottom: 20px;
  left: 70px;
}
.el-form-item {
  margin: 0 30px;
}

.sendBtn {
  width: 200px;
}

.inputInfo {
  font-size: 18px;
}
</style>
